import {Component, Input, OnChanges, OnInit} from '@angular/core';
import {IframeHeight} from "@shared/util/window.util";

@Component({
    selector: 'erupt-iframe',
    template: `
        <nz-spin [nzSpinning]="spin">
            <iframe [src]="url|safeUrl" style="width: 100%;border: 0;" [ngStyle]="style" [id]="iframeId"
                    [name]="iframeId"
                    (load)="iframeHeight($event)">

            </iframe>
        </nz-spin>
    `,
    styles: []
})
export class EruptIframeComponent implements OnInit, OnChanges {

    @Input() url: string;

    @Input() height: string;

    @Input() style: object = {};

    spin: boolean = true;

    iframeId: string = new Date().getTime() + '';

    constructor() {

    }

    ngOnInit() {
        if (!this.iframeId) {
            this.iframeId = new Date().getTime() + '';
        }
        this.spin = true;
    }

    iframeHeight(event) {
        this.spin = false;
        if (!this.height) {
            IframeHeight(event);
        }
       // this.url = "javascript:return false";
       // console.log('iframe加载完成，设置src为false！')
    };

    ngOnChanges(changes: import("@angular/core").SimpleChanges): void {
        if (!changes.url.firstChange) {
            this.spin = true;
        }
    }

    changeIframeHidden() {
        //this.style = {display: 'none'};
        //this.url = "javascript:return false";
        //将iframe的document移动到外部页面
        /*const iframe = document.getElementById(this.iframeId);
        document.getElementsByTagName('body')[0].appendChild((<HTMLIFrameElement>iframe).contentDocument.documentElement);

        console.log('iframe即将销毁！')*/
    }

    changeIframeShow() {
        //this.style = {display: 'block'};
        /*const hs = document.getElementsByTagName('html');
        if (hs && hs.length > 1) {
            let iframe = document.getElementById(this.iframeId);
            iframe.replaceChild(hs[1], (<HTMLIFrameElement>iframe).contentDocument.documentElement);
        }
        console.log('设置iframe显示！')*/
    }

}
